package com.itcast.controller;

import com.itcast.pojo.service.Category;
import com.itcast.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有一级分类
     */
    @GetMapping("/categorys")
    public ResponseEntity<List<Category>> findAllCategory() {
        List<Category> categoryList = categoryService.findAllCategory();
        return ResponseEntity.ok(categoryList);
    }

    /**
     * 条件查询
     */
    @GetMapping("/category")
    public ResponseEntity<List<Category>> findCategory(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam (value = "pid",defaultValue = "0") Integer pid) {
        List<Category> category = categoryService.findCategoryByCodeOrName(code, name,pid);
        return ResponseEntity.ok(category);
    }

    /**
     * 添加
     */
    @PostMapping("/addCategory")
    public ResponseEntity<Void> addCategory(@RequestBody Category category){
        categoryService.addCategory(category);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 删除
     */
    @DeleteMapping("/deleteCategoryById")
    public ResponseEntity<Void> deleteCategory(@RequestParam ("id") Integer id){
        categoryService.deleteCategory(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改分类
     */
    @PostMapping("/updateCategory")
    public ResponseEntity<Void> updateCategory(@RequestBody Category category){
        categoryService.updateCategory(category);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
